package com.itheima.contorller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.domian.*;
import com.itheima.domian.vo.DishVo;
import com.itheima.domian.vo.QueryVo;
import com.itheima.mapper.admin.DishMapper;
import com.itheima.service.admin.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author 12441
 * @Date 2022/8/11 15:00
 * @Version 1.0
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishService dishService;

    @PostMapping
    public R<DishVo> addDish(@RequestBody DishVo dishVo){
        log.info("========="+dishVo.toString());
        dishService.addDish(dishVo);

        return R.success("添加成功");
    }
    @GetMapping("/page")
    public R<IPage> page(HttpServletRequest httpServerRequest){


        String name = httpServerRequest.getParameter("name");
        Integer page1 = Integer.valueOf(httpServerRequest.getParameter("page"));
        Integer size = Integer.valueOf(httpServerRequest.getParameter("pageSize"));

        Page<QueryVo> page = new Page<>(page1, size);

        IPage<QueryVo> queryVoPage = dishService.findByPage(page, new QueryWrapper<>());

        log.info(""+page.getRecords());

        return R.success(queryVoPage,"啊啊啊啊啊啊啊啊啊小风车");
    }
    @DeleteMapping
    public R<DishVo> deleteDish(HttpServletRequest httpServletRequest) {
        Long id = Long.valueOf(httpServletRequest.getParameter("ids"));

        log.info("========="+id);

        dishService.removeById(id);

        return R.success("成功");
    }
    @PostMapping("/status/{status}")
    public R<Dish> statusToStop(@PathVariable Integer status, HttpServletRequest httpServletRequest){
        Long id = Long.valueOf(httpServletRequest.getParameter("ids"));
        System.out.println("=================1"+status);
        log.info("========="+id);

        Dish dish = dishMapper.selectById(id);
        dish.setStatus(1);
        return  R.success(dish,"成功");
    }
//    @GetMapping("/list{categoryId}")
//    public R<List<Dish>> statusToStop(@PathVariable long categoryId, HttpServletRequest httpServletRequest){
//        Long id = Long.valueOf(httpServletRequest.getParameter("ids"));
//        System.out.println("=================1"+categoryId);
//        log.info("========="+id);
//        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<>();
//
//        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
//        List<Dish> list = dishService.list(queryWrapper);
//
//        return  R.success(list,"成功");
//    }
@GetMapping("/list")
public R<List<Dish>> statusToStop(Dish dish){
    LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<>();
    queryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
   log.info("===========================");
    //排序
    queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
    List<Dish> list = dishService.list(queryWrapper);

    return  R.success(list);
}
    @GetMapping("/{id}")
    public R<Dish> viewDish(@PathVariable Long id){

        log.info("===========:{}",id);

        DishVo byIdWithFlavor = dishService.getByIdWithFlavor(id);

        return  R.success(byIdWithFlavor,"成功");
    }
    @PutMapping
    public R<DishVo> UpdateDish(@RequestBody DishVo dishVo){
        log.info("========="+dishVo.toString());
        dishService.updateWithFlavor(dishVo);

        return R.success(dishVo,"成功");
    }

}
